<?php defined('SYSPATH') OR die('No direct access allowed.');

class Forum_Answer_Controller extends Template_Controller {

	const ALLOW_PRODUCTION = FALSE;
	public $template = 'layouts/template_admin_forum';

	public function index(){
		url::redirect("forum/forum_answer/listResultForumAnswer");
	}

	public function forumanswerForm(){
		$this->template->content = new View('forum/forum_answer_create');
		$this->template->content->forum_categories=ORM::factory("forum_category")->find_all();
		$this->template->content->captcha = Captcha::factory();
		$this->template->title = '';

	}

	public function insertForumAnswer(){

		if ($_POST){
			if (Captcha::valid($this->input->post('captcha_response'))){
				$forumAnswer = new Forum_Answer_Model();
				$forumAnswer->answer=$this->input->post("answer");
				$forumAnswer->forum_question_id=$this->input->post("questionId");
				$forumanswer->post_date=Forum_Answer_Controller::getPostDate();
				$forumanswer->user_id=1;
				$forumanswer->save();
				url::redirect("forum/forum_answer/listResultForumAnswer");
			}else{
				$this->template->content = new View('forum/forum_answer_create');
				$this->template->content->captcha = Captcha::factory();
			}
		}else{
			$this->template->content = new View('forum/forum_answer_create');
			$this->template->content->captcha = Captcha::factory();
		}
		$this->template->title = '';

	}

	public function listResultForumAnswer(){
		$this->template->content = new View('forum/forum_answer_listResult');
		$this->template->content->forum_answers=ORM::factory("forum_answer")->find_all();
		$this->template->title = '';

	}

	public function listResultForumAnswerByFQId(){
		if($this->input->get("id")!=null && $this->input->get("id")!=''){
			$this->template->content = new View('forum/forum_answer_listResult');
			$this->template->content->forum_answers=ORM::factory("forum_answer")
			->where('forum_question_id', $this->input->get("id"))
			->find_all();
			$this->template->title = '';
		}
	}

	public function findForumAnswerByFQId(){
		if($this->input->get("id")!=null && $this->input->get("id")!=''){
			$this->template->content = new View('forum/forum_answer_create');
			$this->template->content->forum_questions=ORM::factory("forum_question")
												->where('id', 1)
												->find_all();
			$this->template->title = '';
		}
	}

	function getPostDate(){
		$db = new Database;
		$sql = 'select sysdate() as post_date from dual';
		$query = $db->query($sql);
		$strPostDate="";
		foreach($query as $item){
			$strPostDate = $item->post_date;
			break;
		}
		return $strPostDate;
	}
	public function getGetId(){
		echo "ID : ".$this->input->get("id");
	}

}
?>